//
//  DBHomeViewController.swift
//  Weibo
//
//  Created by zyc on 16/6/4.
//  Copyright © 2016年 heima. All rights reserved.
//

import UIKit

let identifier = "cell"

class DBHomeViewController: DBRootViewController {
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        //在每一个子模块中，使用的自定义cell可能不一样
        tableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: identifier)
    }
    
}

extension DBHomeViewController {
    
    override func loadData() {
        
        if isPullUp {
            
            let delay = dispatch_time(DISPATCH_TIME_NOW, Int64(2.0 * Double(NSEC_PER_SEC)))
            
            self.footRefreshControl.startAnimating()
            
            dispatch_after(delay, dispatch_get_main_queue()){
                
                let end = (self.dataArr.last ?? 981) - 1
                
                let from = end - 19
                
                var requestArr: [Int] = []
                
                for i in (from ... end).reverse() {
                    
                    requestArr.append(i)
                    
                }
                
                self.dataArr += requestArr
                
                self.footRefreshControl.stopAnimating()
                
                self.tableView.reloadData()
                
                self.isPullUp = false
                
            }
            
        } else {
            
            let delay = dispatch_time(DISPATCH_TIME_NOW, Int64(2.0 * Double(NSEC_PER_SEC)))
            
            dispatch_after(delay, dispatch_get_main_queue()) {
                
                let from = (self.dataArr.first ?? 1000) + 1
                
                let end = from + 19
                
                var requestArr: [Int] = []
                
                for i in (from ... end).reverse() {
                    
                    requestArr.append(i)
                    
                }
                
                self.dataArr = requestArr + self.dataArr
                
                self.refreshControl.endRefreshing()
                
                self.tableView.reloadData()
                
            }
            
        }
        
    }
    
}

extension DBHomeViewController {
    
    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        
        return dataArr.count
        
    }
    
    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        
        let cell = tableView.dequeueReusableCellWithIdentifier(identifier, forIndexPath: indexPath)
        
        
        cell.textLabel?.text = "\(dataArr[indexPath.row])"
        
        return cell
        
    }
    
}

extension DBHomeViewController {
    
    override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
        
        
        
    }
    
    override func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {
        
        if indexPath.row == dataArr.count - 1 {
            
            isPullUp = true
            
            loadData()
            
        }
        
    }
    
}